package com.fastadmin.mapper.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.StrUtil;
import com.fastadmin.pojo.Ayi;
import com.fastadmin.pojo.FastParam;
import com.xphsc.easyjdbc.EasyJdbcTemplate;
import com.xphsc.easyjdbc.page.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.xphsc.easyjdbc.core.metadata.ElementResolver;

import java.sql.Timestamp;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class AyiMapper extends BaseMapper {
    @Autowired
    EasyJdbcTemplate easyJdbcTemplate;

    public PageInfo<Ayi> findByFastParam(FastParam fastParam) {
        String tableName = ElementResolver.resolve(Ayi.class).getTable();
        PageInfo<Ayi> pageInfo = this.easyJdbcTemplate.findByPage(fastParam.getSql(tableName), Ayi.class, fastParam.getOffset(), fastParam.getLimit());
        return pageInfo;
    }

    public Ayi findById(Long ids) {
        Ayi ayi = this.easyJdbcTemplate.getByPrimaryKey(Ayi.class, ids);
        return ayi;
    }

    public void deleteById(String id) {
        this.easyJdbcTemplate.deleteByPrimaryKey(Ayi.class, id);
    }

    public void update(Ayi ayi) {
        ayi.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        this.easyJdbcTemplate.update(ayi);
    }

    public Object save(Ayi ayi) {
        ayi.setCreate_time(new Timestamp(System.currentTimeMillis()));
        ayi.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        return this.easyJdbcTemplate.insertKey(ayi);

    }

    public List<Ayi> findByIds(String ids) {
        if (StrUtil.isBlank(ids)) {
            return CollUtil.newArrayList();
        }
        return this.easyJdbcTemplate.findByIds(Ayi.class, Arrays.asList(StrUtil.splitToArray(ids, ',')));

    }

    public void updateMiClick(Long id) {
        String sql = "UPDATE t_ayi SET mi_count=mi_count+1 WHERE id=" + id;
        easyJdbcTemplate.execute(sql);

    }

    public List<Map<String, Object>> findUserAvatar(Long no) {
        StrBuilder sql = StrBuilder.create();
        sql.append(" SELECT ");
        sql.append(" 	f_user.avatar as avatarUrl  ");
        sql.append(" FROM ");
        sql.append(" 	t_history, ");
        sql.append(" 	f_user  ");
        sql.append(" WHERE ");
        sql.append(" 	t_history.openid = f_user.open_id");
        sql.append(" 	AND t_history.ayi_id= ? ");
        List<Map<String, Object>> maps = easyJdbcTemplate.find(sql.toString(), no);
        return maps;
    }

    public PageInfo<Ayi> findHistoryByOpenId(FastParam fastParam, String openid) {
        StrBuilder sql = StrBuilder.create();
        sql.append(" SELECT ");
        sql.append(" 	t_ayi.*  ");
        sql.append(" FROM ");
        sql.append(" 	t_ayi, ");
        sql.append(" 	t_history  ");
        sql.append(" WHERE ");
        sql.append(" 	t_ayi.id = t_history.ayi_id  ");
        sql.append(" 	AND t_history.openid = ? ");
        sql.append(" 	ORDER BY  t_history.id DESC");
        return easyJdbcTemplate.findByPage(sql.toString(), Ayi.class, fastParam.offset, fastParam.limit, openid);
    }

    public PageInfo<Ayi> findKeep1ByOpenId(FastParam fastParam, String openid) {
        StrBuilder sql = StrBuilder.create();
        sql.append(" SELECT ");
        sql.append(" 	t_ayi.*  ");
        sql.append(" FROM ");
        sql.append(" 	t_ayi, ");
        sql.append(" 	t_keep  ");
        sql.append(" WHERE ");
        sql.append(" 	t_ayi.id = t_keep.ayi_id  ");
        sql.append(" 	AND t_keep.openid = ? ");
        sql.append(" 	AND t_keep.type = 1 ");
        sql.append(" 	ORDER BY  t_keep.id DESC");
        return easyJdbcTemplate.findByPage(sql.toString(), Ayi.class, fastParam.offset, fastParam.limit, openid);
    }

}
